package com.cy.interceptor;

import com.cy.utils.SimpleHttpUtils;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.cy.shiro.ShiroUser;
import org.apache.log4j.MDC;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;


/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-18
 * Time: 下午5:18
 * 后台常规日志记录
 */
public class EventLogInterceptor implements Interceptor {

    public static final String EventLogName = "eventLogger";

    @Override
    public void intercept(ActionInvocation ai) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            MDC.put("user_uuid", ((ShiroUser) subject.getPrincipal()).getId());
        } else {
            MDC.put("user_uuid", 0);
        }
        MDC.put("type", 0);
        MDC.put("_startTime", String.valueOf(System.currentTimeMillis()));
        MDC.put("ip", SimpleHttpUtils.getIpAdress(ai.getController().getRequest()));
        MDC.put("action", ai.getController().getClass().getName());
        MDC.put("method", ai.getMethodName());
        ai.invoke();
    }

}
